package com.prj.ufdm.cloud.ms.core.config;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.alibaba.druid.filter.logging.Slf4jLogFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

/**
 * 配置auth数据源
 * @author Administrator
 */
@Configuration
public class MainDataSourceConfig {
	
    @Value("${main.ds.driverClassName}")
    private String mainDriverClassName;
    @Value("${main.ds.url}")
    private String mainDsUrl;
    @Value("${main.ds.username}")
    private String mainDsUsername;
    @Value("${main.ds.password}")
    private String mainDsPassword;
    
    @Value("${main.ds.initialSize:3}")
    private String mainDsInitialSize;
    @Value("${main.ds.minIdle:3}")
    private String mainDsMinIdle;
    @Value("${main.ds.maxActive:10}")
    private String mainDsMaxActive;
    @Value("${main.ds.validationQuery:select 1 from dual}")
    private String mainDsValidationQuery;
    @Value("${main.ds.statementExecutableSqlLogEnable:false}")
    private String mainDsStatementExecutableSqlLogEnable;
    
    
    @Bean(name = "mainDsDataSource") 
    @Primary
    public DataSource mainDataSource() throws SQLException {
    	
        DruidDataSource basicDataSource = new DruidDataSource();
        basicDataSource.setDriverClassName(mainDriverClassName);
        basicDataSource.setUrl(mainDsUrl);
        basicDataSource.setUsername(mainDsUsername);
        basicDataSource.setPassword(mainDsPassword);
		try {
			basicDataSource.setInitialSize(Integer.parseInt(mainDsInitialSize));
		} catch (Exception er) {
			basicDataSource.setInitialSize(3);
		}
		try {
			basicDataSource.setMinIdle(Integer.parseInt(mainDsMinIdle));
		} catch (Exception er) {
			basicDataSource.setMinIdle(3);
		}
		try {
			basicDataSource.setMaxActive(Integer.parseInt(mainDsMaxActive));
		} catch (Exception er) {
			basicDataSource.setMaxActive(20);
		}
        basicDataSource.setTestWhileIdle(true);
        basicDataSource.setValidationQuery(mainDsValidationQuery);
        basicDataSource.setTestOnReturn(true);
        basicDataSource.setTestOnBorrow(false);
        basicDataSource.setTimeBetweenEvictionRunsMillis(30000);
        List<com.alibaba.druid.filter.Filter> listFilter = new ArrayList<com.alibaba.druid.filter.Filter>();
        
        System.out.println("\n 是否开启："+mainDsStatementExecutableSqlLogEnable);
        
        if("true".equals(mainDsStatementExecutableSqlLogEnable)){
            Slf4jLogFilter slf4jLogFilter = new Slf4jLogFilter();
            slf4jLogFilter.setStatementExecutableSqlLogEnable(true);
            listFilter.add(slf4jLogFilter);
        }
        basicDataSource.setProxyFilters(listFilter);
        
        System.out.println("\n mainDs 主数据启动，配置：" + basicDataSource.getUrl() + "; driverClassName="+mainDriverClassName);
        
        return basicDataSource;
        
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        Map<String,String> initParams = new HashMap<>();
        initParams.put("loginUsername","admin");
        initParams.put("loginPassword","123456");
        initParams.put("allow","");
        initParams.put("deny","192.168.255.255");
        bean.setInitParameters(initParams);
        return bean;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
	@Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());
        Map<String,String> initParams = new HashMap<>();
        initParams.put("exclusions","*.js,*.css,/druid/*");
        bean.setInitParameters(initParams);
        bean.setUrlPatterns(Arrays.asList("/*"));
        return  bean;
    }
}
